<?php
/************************* Handle Taxonomies and Custom Post Types *************************/

// define custom icon for custom post type in Dashboard
$ico_path = get_template_directory_uri() . '/images/catalogue.png';

// define meta-box arguments/params for product definition
$args_array = array(
	'label' => __( 'Products' ),
	'singular_label' => __( 'Product' ),
	'public' => true,
	'show_ui' => true,
	'menu_icon' => $ico_path,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => true,
	'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'comments' ),
	'taxonomies' => array( 'post_tag' )
);

// call helper function for the SD_Register_Post_Type class
sd_register_post_type( 'product', $args_array, 'products' );

// add taxonomy aka custom post type category
function sofa_reg_taxonomy() {
	register_taxonomy( 'catalog', array( 'product' ), array( 'hierarchical' => true, 'label' => 'Catalogs', 'singular_label' => 'Catalog', 'rewrite' => true ) );
	/*register_taxonomy_for_object_type( 'catalog', 'product' );*/
}
add_action( 'init', 'sofa_reg_taxonomy', 0 );


// array of meta-boxes regardless the amount
$meta_boxes = array();
god_father( 'sofa_' );

// generate meta boxes
function god_father( $pre_fix = 'sofa_' ) {
	$cnt_mb = 0;
	$aOptions = SofaSuppaStore::initOptions( false );
	$num_of_mb = $aOptions[ 'num_of_def_boxes' ];
	if( $num_of_mb <= 0 ) $num_of_mb = 1;
	while( $cnt_mb < $num_of_mb ) {
		$increaser = $cnt_mb + 1;
		$prefix = $pre_fix . $cnt_mb . '_';
		$mb_id = 'product-mbox-' . $cnt_mb;
		$mb_ti = 'Product Definition Box #' . $increaser;
		$meta_boxes[] = array(
			'id' => $mb_id,
			'title' => $mb_ti,
			'pages' => array('product'),
			'context' => 'normal',
			'priority' => 'high',
			'fields' => array(
			array(
			'name' => 'Price',
			'desc' => 'Add product price (No currency code at the beginning! Must be floating point number, for example: 13298.95, 21.00, 124800.00)',
			'id' => $prefix . 'textarea_price',
			'type' => 'text',
			'std' => '*'
			),
			array(
			'name' => 'Size',
			'desc' => 'Add product size (Do not use ~, /, \, <, >, *, $, %, single or double quotes)',
			'id' => $prefix . 'textarea_size',
			'type' => 'text',
			'std' => '*'
			),
			array(
			'name' => 'Stock',
			'desc' => 'Number of products in stock (Must be number - integer!). Enter * (asterisk) if Stock Inventory management is not required by your shop!',
			'id' => $prefix . 'textarea_stock',
			'type' => 'text',
			'std' => '*'
			),
			array(
			'name' => 'Shipping Price',
			'desc' => 'Enter * (asterisk) if you don\'t want to declare individual shipping price! Global shipping price can be declared at theme Options page.',
			'id' => $prefix . 'textarea_shipp',
			'type' => 'text',
			'std' => '*'
			)
			)
		);
		$cnt_mb ++;
	}
	if( $cnt_mb > 0 ) {
		// add extra meta-box for product thumbnail label
		$meta_boxes[] = array(
			'id' => 'product-mbox-' . ( $cnt_mb + 1 ),
			'title' => 'Thumbnail Label, Featured Product?',
			'pages' => array('product'),
			'context' => 'side',
			'priority' => 'low',
			'fields' => array(
			array(
			'name' => 'Thumbnail Label Value',
			'desc' => 'Enter whatever you like (for example: Reduced, New Model, Limited Edition)',
			'id' => 'labelvalue',
			'type' => 'text',
			'std' => ''
			),
			array(
			'name' => 'Thumbnail label type',
			'id' => 'labeltype',
			'type' => 'select',
			'options' => array(
				'' => ' -- Select label type -- ',
				'reduced' => ' -- Reduced -- ',
				'newmodel' => ' -- New Model -- ',
				'limited' => ' -- Limited Edition -- '
				)
			),
			array(
			'name' => 'Is this Featured Product?',
			'desc' => 'Get it listed at Home Page.',
			'id' => 'featured',
			'type' => 'checkbox'
			)
			)
		);
		foreach ( $meta_boxes as $meta_box ) {
			$my_box = new RW_Meta_Box( $meta_box );
		}
	}
}

function prod_edit_columns( $columns ) {
		// these should correspond to meta-box names
		$columns = array(
			'cb' => '<input type="checkbox" />',
			'title' => 'Product Title',
			'price' => 'Price(s)',
			'shipp' => 'Shipping Price',
			'size' => 'Size(s)',
			'stock' => 'In Stock',
			'catalog' => 'Catalog',
			'thumb' => 'Image',
		);

		return $columns;
}
function prod_custom_columns( $column ) {
		global $post, $meta_boxes;
		$aOptions = SofaSuppaStore::initOptions( false );
		$num_of_mb = $aOptions[ 'num_of_def_boxes' ];
		if( $num_of_mb <= 0 ) $num_of_mb = 1;
		switch( $column )
		{
			case 'price':
				$custom = get_post_custom();
				$mb_arr_len = count( $meta_boxes );
				$cnt_pr = 0;
				$out_pr = '';
				while( $cnt_pr < $num_of_mb ) {
					$out_pr .= $custom[ 'sofa_' . $cnt_pr . '_textarea_price' ][ 0 ] . '<br />';
					$cnt_pr ++;
				}
				echo $out_pr;
				break;
			case 'shipp':
				$custom = get_post_custom();
				$mb_arr_len = count( $meta_boxes );
				$cnt_ss = 0;
				$out_ss = '';
				while( $cnt_ss < $num_of_mb ) {
					$out_ss .= $custom[ 'sofa_' . $cnt_ss . '_textarea_shipp' ][ 0 ] . '<br />';
					$cnt_ss ++;
				}
				echo $out_ss;
				break;
			case 'size':
				$custom = get_post_custom();
				$mb_arr_len = count( $meta_boxes );
				$cnt_si = 0;
				$out_si = '';
				while( $cnt_si < $num_of_mb ) {
					$out_si .= $custom[ 'sofa_' . $cnt_si . '_textarea_size' ][ 0 ] . '<br />';
					$cnt_si ++;
				}
				echo $out_si;
				break;
			case 'stock':
				$custom = get_post_custom();
				$mb_arr_len = count( $meta_boxes );
				$cnt_st = 0;
				$out_st = '';
				while( $cnt_st < $num_of_mb ) {
					$fld = $custom[ 'sofa_' . $cnt_st . '_textarea_stock' ][ 0 ];
					/* WORDPRESS bug (will be fixed for WP 3.2) - meta value can't be zero! http://core.trac.wordpress.org/ticket/15292 */
					$chck_price = $custom[ 'sofa_' . $cnt_st . '_textarea_price' ][ 0 ];
					$chck_size  = $custom[ 'sofa_' . $cnt_st . '_textarea_size' ][ 0 ];
					/* END */
					$out_st .= ( ( strlen( $fld ) == 0 && strlen( $chck_price ) != 0 && strlen( $chck_size ) != 0 ) ? '0' : $fld ) . '<br />';
					$cnt_st ++;
				}
				echo $out_st;
				break;
			case 'catalog':
				echo get_the_term_list( $post->ID, 'catalog', '', ', ','' );
				break;
			case 'thumb':
				$args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' );
				$attachment = get_posts( $args );
				if( $attachment ) {
					$img = wp_get_attachment_thumb_url( $attachment[ 0 ]->ID );
					echo '<img src="' . $img . '" style="width: 70px; height: auto;" />';
				}
				break;
		}
}
// hook admin overview
add_filter( 'manage_edit-product_columns', 'prod_edit_columns' );
add_action( 'manage_posts_custom_column',  'prod_custom_columns' );

// add custom post type for jQuery nivo images slider
// define custom icon for custom post type in Dashboard
$is_ico_path = get_template_directory_uri() . '/images/is_slider.png';

// define meta-box arguments/params for product definition
$is_args_array = array(
	'label' => __( 'Nivo-slides' ),
	'singular_label' => __( 'Nivo-slide' ),
	'public' => true,
	'show_ui' => true,
	'menu_icon' => $is_ico_path,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => true,
	'supports' => array( 'title', 'thumbnail' )
);

// call helper function for the SD_Register_Post_Type class
sd_register_post_type( 'nivo-slide', $is_args_array, 'nivo-slides' );

// metabox
$is_meta_boxes = array();
// define metabox for Nivo slider slides
$is_meta_boxes[] = array(
	'id' => 'is-mbox',
	'title' => 'Upload images for jQuery Nivo images slider',
	'pages' => array( 'nivo-slide' ),
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array(
			'name' => 'Image slide ( should be 928x348px )',
			'desc' => 'Crop your image to exact dimension before upload!',
			'id' => 'isslide',
			'type' => 'image'
		),
		array(
		'name' => 'Caption text',
		'desc' => 'The above entered text will be displayed as image caption (tool tip for Piecemaker!).',
		'id' => 'caption_text',
		'type' => 'text'
		)
	)
);
// create meta boxes
foreach ( $is_meta_boxes as $is_meta_box ) {
	$is_my_box = new RW_Meta_Box( $is_meta_box );
}
// define Admin overview
function is_prod_edit_columns( $columns ) {
		// these should correspond to meta-box names
		$columns = array(
			'cb' => '<input type="checkbox" />',
			'title' => 'Nivo Slide Title',
			'thumb' => 'Image',
		);

		return $columns;
}
// hook admin overview
add_filter( 'manage_edit-nivo-slide_columns', 'is_prod_edit_columns' );